package com.android.equipment.ui.dialog

import android.content.Context
import com.android.equipment.R
import com.android.equipment.adapter.DeviceNameAdapter
import com.android.equipment.bean.WorkDetailBean
import com.android.equipment.bean.WorkDetailDeviceData
import com.android.equipment.bean.WorkDetailPeopleData
import com.android.equipment.dao.Device
import com.android.equipment.dao.People
import com.android.equipment.dao.WorkDetail
import com.android.equipment.dao.operate.DaoConfigModel
import com.android.equipment.dao.operate.DaoDevice
import com.android.equipment.dao.operate.DaoPeople
import com.android.equipment.dao.operate.DaoWork
import com.android.equipment.dao.operate.DaoWorkDetail
import com.android.equipment.databinding.DialogContactBinding
import com.android.equipment.utils.ToastUtil
import com.android.equipment.utils.UiDataHandler
import com.blankj.utilcode.util.GsonUtils
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView

class ContactDialog(
    context: Context,
    val workDetailBeanList: MutableList<WorkDetail>,
    val workId: Long,
    private var listener: OnConfirmListener,
) : CenterPopupView(context) {
    lateinit var binding: DialogContactBinding
    private var selectPeopleIndex: Int? = null
    private var selectDeviceIndex: Int? = null

    private var allPeopleList: List<People>?= null
    private var allDeviceList: List<Device>? = null

    private var currentPeople: WorkDetailPeopleData?= null
    private val currentDeviceList: MutableList<WorkDetailDeviceData> by lazy {
        mutableListOf()
    }

    private val peopleList: MutableList<WorkDetailPeopleData> by lazy {
        mutableListOf()
    }

    private val deviceList: MutableList<WorkDetailDeviceData> by lazy {
        mutableListOf()
    }

    private val newAddWorkDetailList: MutableList<WorkDetail> by lazy {
        mutableListOf()
    }

    private val deviceNameAdapter: DeviceNameAdapter by lazy {
        DeviceNameAdapter(R.layout.item_device_name)
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_contact
    }

    override fun onCreate() {
        super.onCreate()
        binding = DialogContactBinding.bind(popupImplView)
        initData()
        initEvent()
        initRecycler()
    }

    private fun initRecycler() {
        val flex = FlexboxLayoutManager(context).apply {
            flexDirection = FlexDirection.ROW
            flexWrap = FlexWrap.WRAP
            justifyContent = JustifyContent.FLEX_START
        }
        binding.rvDeviceName.layoutManager = flex
        binding.rvDeviceName.adapter = deviceNameAdapter
        deviceNameAdapter.setNewInstance(currentDeviceList)
        deviceNameAdapter.setOnItemClickListener { adapter, view, position ->
            deviceNameAdapter.removeAt(position)
        }
    }

    fun initData() {
        allPeopleList = DaoPeople.getInstance().allPeople
        val exitPeopleIds = workDetailBeanList.map { it.peopleId }.distinct()
        allPeopleList?.forEach {
            val peopleData = WorkDetailPeopleData(peopleId = it.id, peopleName = it.peopleName, peoplePhoto = it.peoplePhoto, peoplePhone = it.peoplePhone)
            if (exitPeopleIds.contains(it.id)) {
                peopleData.isExit = true
            }
            peopleList.add(peopleData)
        }

        allDeviceList = DaoDevice.getInstance().allDevice
        val exitDevices = workDetailBeanList.map { it.deviceId }
        allDeviceList?.forEach {
            val deviceData = WorkDetailDeviceData(deviceId = it.id, deviceCode = it.deviceCode, deviceName = it.deviceName, deviceModelId = it.deviceModelId, alarmData  = DaoConfigModel.getInstance().getConfigModelByDeviceModelId(it.deviceModelId) )
            if (exitDevices.contains(it.id)){
                deviceData.isExit = true
            }
            deviceList.add(deviceData)
        }

    }

    fun initEvent() {
        binding.tvContactPeople.setOnClickListener {
            XPopup.Builder(context)
                .asCustom(SelectPeopleDialog(context, peopleList,object : SelectPeopleDialog.SelectPeopleListener{
                    override fun onSelectPeopleClick(people: WorkDetailPeopleData) {
                        currentPeople = people
                        binding.tvContactPeople.text = people.peopleName
                    }

                })).show()



        }

        binding.llContactDevice.setOnClickListener {
            XPopup.Builder(context)
                .asCustom(SelectDeviceDialog(context, deviceList,object : SelectDeviceDialog.SelectDeviceListener{
                    override fun onSelectDeviceClick(device: WorkDetailDeviceData) {
                        deviceNameAdapter.addData(device)
                    }

                })).show()

        }

        binding.tvConfirm.setOnClickListener {
            if (currentPeople == null || currentDeviceList.isEmpty()) {
                ToastUtil.showToastSafe("请选择人员或设备")
                return@setOnClickListener
            }
            newAddWorkDetailList.clear()
            currentDeviceList.forEach {
                val workDetail = WorkDetail()
                workDetail.workId = workId
                workDetail.workName = DaoWork.getInstance().getWork(workId).name
                workDetail.peopleId = currentPeople!!.peopleId
                workDetail.peopleName = currentPeople!!.peopleName
                workDetail.peoplePhone = currentPeople!!.peoplePhone
                workDetail.peoplePhoto = currentPeople!!.peoplePhoto
                workDetail.deviceId = it.deviceId
                workDetail.deviceCode = it.deviceCode
                workDetail.deviceName = it.deviceName
                workDetail.deviceModelId = it.deviceModelId

                val defaultNodeData = UiDataHandler.handlerDefaultNodeData(it.deviceModelId)
                workDetail.deviceNodeJson = GsonUtils.toJson(defaultNodeData)

                newAddWorkDetailList.add(workDetail)
            }

            dismiss();
            listener.onConfirm(newAddWorkDetailList)
        }

        binding.tvCancel.setOnClickListener {
            dismiss()
        }
    }

    interface OnConfirmListener {
        fun onConfirm(newWorkDetailList: MutableList<WorkDetail>)
    }
}